package com.learn.controller;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.learn.entity.BookEntity;
import com.learn.service.BookService;
import com.learn.utils.PageUtils;
import com.learn.utils.Query;
import com.learn.utils.R;


/**
 * 产品信息
 */
@RestController
    //处理请求地址映射的注解
@RequestMapping("book")
    //子类扩展父类
public class BookController extends AbstractController {
    @Autowired
    private BookService bookService;

    /**
     * 列表
     */
    //请求路径的注解
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {


        //查询列表数据
        Query query = new Query(params);

        List<BookEntity> bookList = bookService.queryList(query);
        int total = bookService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(bookList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 列表
     */
    //@RequestParam指定request中必须包含某些参数值时，才让该方法处理。
    @RequestMapping("/list2")
    public R list2(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        List<BookEntity> bookList = bookService.queryList(query);
        return R.ok().put("list", bookList);
    }


    /**
     * 信息
     */
    //@PathVariable接收请求路径中占位符的值
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        BookEntity book = bookService.queryObject(id);

        return R.ok().put("book", book);
    }

    /**
     * 保存
     */
    //@RequestBody主要用来接收前端传递给后端的 json 格式的数据的
    @RequestMapping("/save")
    public R save(@RequestBody BookEntity book) {

        book.setState("在库");

        bookService.save(book);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody BookEntity book) {
        bookService.update(book);

        return R.ok();
    }

    /**
     * 删除
     */
    //Requestbody：返回json格式
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        bookService.deleteBatch(ids);

        return R.ok();
    }

    public static String CODE =null;


}
